#!/bin/dash

#Copyright (c) 2014 Luigi Tarenga <luigi.tarenga@gmail.com>
#Distributed under the terms of a MIT license.

export LANG=C
export sshopt="-o ControlPath=~/.ssh/lite-raft-socket-%h -o ControlMaster=auto \
-o ConnectTimeout=1 -o ServerAliveInterval=2 -o ServerAliveCountMax=3 \
-o PasswordAuthentication=no -o StrictHostKeyChecking=no -q"

cd ${0%/*}

read server_role      < temp/server_role
read hostname         < temp/hostname
read current_leader   < temp/current_leader
read cluster_nodes    < conf/cluster_nodes

if [ -d temp/master_lockdir ] ; then
   if [ "$server_role" = "leader" ] ; then
      case "$1" in
      if|set|unset)
         if [ "$1" != "if" -a -z "$2" -o "$1" = "if" -a "$5" != "set" ] ; then
            echo missing argument >&2
            exit 10
         fi
         internals/activity-lock-ex internals/client "$@"
      ;;
      noop|no-op)
         internals/activity-lock-ex internals/client "noop"
      ;;
      get)
         internals/activity-lock-sh internals/client get "$2"
      ;;
      list)
         internals/activity-lock-sh internals/client list "$2"
      ;;
      status)
         read last_log_index < state/last_log_index
         echo "Leader:  $hostname   log index: $last_log_index"
         for h in $cluster_nodes ; do
            [ "$h" = "$hostname" ] && continue
            read match_index < temp/${h}_match_index
            echo "Follower: $h   log index: $match_index"
         done
      ;;
      add-member|del-member)
         if [ -z "$2" ] ; then
            echo missing argument >&2
            exit 10
         fi
         read cluster_nodes < conf/cluster_nodes
         cluster_nodes_new="$cluster_nodes $2"
         if [ "$1" = "del-member" ] ; then
            #use printf instead of echo -n since hp-ux does not support echo -n (have print instead)
            cluster_nodes_new=$(for h in $cluster_nodes; do [ "$h" != "$2" ] && printf "$h "; done)
         fi
         [ "$1" = "add-member" ] && ssh $sshopt $2 "cd $PWD ; ./lite-raft first-boot"
         echo inserting conf_old_new.
         if internals/activity-lock-ex internals/client conf_old_new "$cluster_nodes_new" ; then
            echo conf_old_new committed.
            echo inserting conf_new.
            if internals/activity-lock-ex internals/client conf_new ; then
               echo conf_new committed.
               [ "$1" = "del-member" ] && ssh $sshopt $2 "cd $PWD ; ./lite-raft stop"
            else
               exit $?
            fi
         else
            exit $?
         fi
      ;;
      *)
         echo "Usage: ${0##*/} get [ key ]"
         echo "                set { key } { value }"
         echo "                unset { key }"
         echo "                status | list | noop | no-op"
         echo "                if { key } { binary-test-operator } { value } set { new-value }"
         echo "                add-member { new-member }"
         echo "                del-member { old-member }"
      ;;
      esac

   elif [ -n "$current_leader" ] ; then
      ssh $sshopt $current_leader "cd $PWD ; ./lite-raft-client '$1' '$2' '$3' '$4' '$5' '$6'"
      exit $(($?+50))
   else
      echo leader is not available.
      exit 11
   fi
else
   #no process running here, return error
   echo no server running on $hostname. cannot process request.
   exit 12
fi
